package com.atguigu.rabbitmq.utlis;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeoutException;

@SuppressWarnings("all")
public class RabbitMQUtils {

    private static Properties rabbitmqProperties;

    private static String HOST;

    private static String USERNAME;

    private static String PASSWORD;

    private static String QUEUE_NAME;

    // 读取rabbitmq的连接参数文件
    static {
        rabbitmqProperties = new Properties();
        try {
            rabbitmqProperties.load(new FileInputStream("/Users/weixiaodong/java_work/atguigu_RabbitMQ/RabbitMQ_HelloWorld/src/main/resources/rabbitmq/rabbitmq_connection.properties"));

            HOST = rabbitmqProperties.getProperty("host");

            USERNAME = rabbitmqProperties.getProperty("username");

            PASSWORD = rabbitmqProperties.getProperty("password");

            QUEUE_NAME = rabbitmqProperties.getProperty("queue_name");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Channel getChannel() {
        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost(HOST);

        factory.setUsername(USERNAME);

        factory.setPassword(PASSWORD);

        Connection connection = null;

        Channel channel = null;

        try {
            connection = factory.newConnection();

            channel = connection.createChannel();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

        return channel;
    }
}
